<?php
/**
 * Created by mww
 * User: mww
 */

namespace app\admin\controller;
use think\Db;
use think\Request;

class Sae extends Base
{
    /**
     * 首页
     */
    public function index(){
        if(request()->isAjax()){
            extract(input());
            $where = [
                'sae_send_uid' => ['>',0]
            ];
            if(session("company_id") > 0){
                $where['sae_c_id'] = session("company_id");
            }
            if(isset($jump_uid) && $jump_uid){
                $where['sae_send_uid'] = $jump_uid;
            }
            if(isset($user_name) && $user_name != ''){
                $where['sae_name|user_username|cp_name|c_name'] = ['like', '%' . $user_name . '%'];
            }
            if(isset($user_type) && $user_type != ''){
                $where['user_type'] = $user_type;
            }
            if(isset($sae_submit) && $sae_submit != ''){
                $where['sae_submit'] = $sae_submit;
            }
            if(isset($sae_answer) && $sae_answer != ''){
                $where['sae_answer'] = $sae_answer;
            }
            if(isset($sae_zhuan) && $sae_zhuan != ''){
                $where['sae_zhuan'] = $sae_zhuan;
            }
            //开始时间查询
            if(isset($start) && $start != "" && isset($end) && $end == "")
            {
                $where['sae_time'] = ['>=',$start];
            }
            if(isset($end) && $end != "" && isset($start) && $start == "")
            {
                $where['sae_time'] = ['<=',$end];
            }
            if(isset($start) && $start != "" && isset($end) && $end != "")
            {
                $where['sae_time'] = ['between',[$start,$end]];
            }
            //结束时间查询
            if(isset($loginstart) && $loginstart != "" && isset($loginend) && $loginend == "")
            {
                $where['sae_submit_time'] = ['>=',$loginstart];
            }
            if(isset($loginend) && $loginend != "" && isset($loginstart) && $loginstart == "")
            {
                $where['sae_submit_time'] = ['<=',$loginend];
            }
            if(isset($loginstart) && $loginstart != "" && isset($loginend) && $loginend != "")
            {
                $where['sae_submit_time'] = ['between',[$loginstart,$loginend]];
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $field = 'sae.*,user_username,user_image,user_phone,user_type,cp_name,c_name,c_charge_name';
            $count = Db::table('up_sae')->alias('sae')
                ->join('up_u_user u','sae.sae_send_uid = u.user_id','left')
                ->join('up_company_product cp','sae.sae_cp_id = cp.cp_id','left')
                ->join('up_company c','sae.sae_c_id = c.c_id','left')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::table('up_sae')->alias('sae')
                ->join('up_u_user u','sae.sae_send_uid = u.user_id','left')
                ->join('up_company_product cp','sae.sae_cp_id = cp.cp_id','left')
                ->join('up_company c','sae.sae_c_id = c.c_id','left')
                ->where($where)
                ->page($NowPage,$limits)
                ->order('sae_update desc')
                ->field($field)
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $jump_uid = input('jump_uid');
        if(!isset($jump_uid) || !$jump_uid){
            $jump_uid = '';
        }
        $this->assign('jump_uid',$jump_uid);
        return $this->fetch();
    }

    /**
     * sae详情
     * @return mixed|\think\response\Json
     */
    public function saeInfo(){
        $sae_id = input('sae_id');
        $field = 'sae.*,user_username,user_image,user_phone,user_type,cp_name,c_name,c_charge_name';
        $info = Db::table('up_sae')->alias('sae')
            ->join('up_u_user u','sae.sae_send_uid = u.user_id','left')
            ->join('up_company_product cp','sae.sae_cp_id = cp.cp_id','left')
            ->join('up_company c','sae.sae_c_id = c.c_id','left')
            ->where('sae_id',$sae_id)
            ->field($field)
            ->find();
        if($info['sae_imgs']){
            $info['sae_imgs'] = explode(',',$info['sae_imgs']);
        }else{
            $info['sae_imgs'] = [];
        }
        $info['sae_sick'] = explode('|||||',$info['sae_sick']);
        $info['sae_other_drug'] = explode('|||||',$info['sae_other_drug']);
        $info['sae_result'] = explode('|||||',$info['sae_result']);
        $info['sae_desc'] = explode('|||||',$info['sae_desc']);
        if($info['sae_zhuan'] == 2){
            $uids = trim($info['sae_zhuan_ids'],',');
            $info['zhuans'] = Db::table('up_u_user')
                ->where('user_id','in',$uids)
                ->field('user_id,user_username,user_image,user_phone')
                ->select();
        }
        $this->assign([
            'info' => $info
        ]);
        return $this->fetch('sae/saeinfo');
    }
    /**
     * 查看回复
     * @return mixed|\think\response\Json
     */
    public function seeReplys(){
        $sae_id = input('sae_id');
        //回复
        $field = 'sr.*,user_id,user_username,user_image,user_phone,user_type';
        $reply = Db::table('up_sae_reply sr')
            ->join('up_u_user u1','sr.sr_user_id=u1.user_id','LEFT')
            ->where('sr_sae_id',$sae_id)
            ->where('sr_show',1)
            ->where('sr_fid',0)
            ->order('sr_time desc')
            ->field($field)
            ->select();
        if($reply){
            foreach ($reply as $k2 => $v2){
                if($v2['sr_imgs']){
                    $reply[$k2]['sr_imgs'] = explode(',',$v2['sr_imgs']);
                }else{
                    $reply[$k2]['sr_imgs'] = [];
                }
                $reply_son = Db::table('up_sae_reply sr')
                    ->join('up_u_user u1','sr.sr_user_id=u1.user_id','LEFT')
                    ->where('sr_fid',$v2['sr_id'])
                    ->order('sr_time desc')
                    ->field($field)
                    ->select();
                if($reply_son){
                    foreach ($reply_son as $k3 => $v3) {
                        if ($v3['sr_imgs']) {
                            $reply_son[$k3]['sr_imgs'] = explode(',', $v3['sr_imgs']);
                        } else {
                            $reply_son[$k3]['sr_imgs'] = [];
                        }
                    }
                }
                $reply[$k2]['son'] = $reply_son;
            }
        }
        $this->assign([
            'reply' => $reply
        ]);
        return $this->fetch('sae/reply');
    }
    /**
     * 如果转给了同事，查看转让同事信息
     */
    public function zhuanUsers(){
        if(request()->isAjax()){
            extract(input());
            $where = [
                'user_id' => ['in',trim($uids,',')]
            ];
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $field = 'user_id,user_username,user_sex,user_image,user_phone';
            $count = Db::table('up_u_user')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::table('up_u_user')
                ->where($where)
                ->page($NowPage,$limits)
                ->field($field)
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $uids = input('uids');
        $this->assign([
            'uids' => trim($uids,',')
        ]);
        return $this->fetch('sae/users');
    }
    /**
     * 导出Excel
     * @return \think\response\Json
     */
    public function outExcel(){
        extract(input());
        set_time_limit(0);//防止超时
        $where = [
            'sae_send_uid' => ['>',0]
        ];
        if(session("company_id") > 0){
            $where['sae_c_id'] = session("company_id");
        }
        if(isset($user_name) && $user_name != ''){
            $where['sae_name|user_username|cp_name|c_name'] = ['like', '%' . $user_name . '%'];
        }
        if(isset($user_type) && $user_type != ''){
            $where['user_type'] = $user_type;
        }
        if(isset($sae_submit) && $sae_submit != ''){
            $where['sae_submit'] = $sae_submit;
        }
        if(isset($sae_answer) && $sae_answer != ''){
            $where['sae_answer'] = $sae_answer;
        }
        if(isset($sae_zhuan) && $sae_zhuan != ''){
            $where['sae_zhuan'] = $sae_zhuan;
        }
        //开始时间查询
        if(isset($start) && $start != "" && isset($end) && $end == "")
        {
            $where['sae_time'] = ['>=',$start];
        }
        if(isset($end) && $end != "" && isset($start) && $start == "")
        {
            $where['sae_time'] = ['<=',$end];
        }
        if(isset($start) && $start != "" && isset($end) && $end != "")
        {
            $where['sae_time'] = ['between',[$start,$end]];
        }
        //结束时间查询
        if(isset($loginstart) && $loginstart != "" && isset($loginend) && $loginend == "")
        {
            $where['sae_submit_time'] = ['>=',$loginstart];
        }
        if(isset($loginend) && $loginend != "" && isset($loginstart) && $loginstart == "")
        {
            $where['sae_submit_time'] = ['<=',$loginend];
        }
        if(isset($loginstart) && $loginstart != "" && isset($loginend) && $loginend != "")
        {
            $where['sae_submit_time'] = ['between',[$loginstart,$loginend]];
        }
        if($ids){
            if($ids != 'all'){
                $where['sae_id'] = ['in',$ids];
            }
        }else{
            return json(['code'=>0,'msg'=>'请选择数据！']);
        }
        $field = [
            'sae_id','user_username','user_image','user_phone', 'user_type',
            'sae_time','sae_submit','sae_submit_time','cp_name','c_name',
            'sae_answer','sae_zhuan','sae_zhuan_ids','sae_name','sae_sex',
            'sae_birthday','sae_phone','sae_sick','sae_spec','sae_use_date',
            'sae_use_many','sae_other_drug','sae_event_name','sae_happend_date','sae_result',
            'sae_desc','sae_imgs','sae_radio','sae_radio_second','sae_video',
            'sae_video_second','sae_id reply'
        ];
        $data =  Db::table('up_sae')->alias('sae')
            ->join('up_u_user u','sae.sae_send_uid = u.user_id','left')
            ->join('up_company_product cp','sae.sae_cp_id = cp.cp_id','left')
            ->join('up_company c','sae.sae_c_id = c.c_id','left')
            ->where($where)
            ->order('sae_update desc')
            ->field($field)
            ->select();
        if($data){
            foreach ($data as $key => $vo){
                //1、转让信息
                if($vo['sae_zhuan'] == 2){
                    $uids = trim($vo['sae_zhuan_ids'],',');
                    $names = Db::table('up_u_user')
                        ->where('user_id','in',$uids)
                        ->column('concat(user_username,"(",user_phone,")") names');
                    $data[$key]['sae_zhuan_ids'] = implode($names,',');
                }
                //2、回复信息
                $replys = Db::table('up_sae_reply sr')
                    ->join('up_u_user u1','sr.sr_user_id=u1.user_id','LEFT')
                    ->where('sr_sae_id',$vo['sae_id'])
                    ->where('sr_show',1)
                    ->column('concat("(",user_username,"，",user_phone,"，",sr_time,"，",sr_content,")") replys');
                if($replys){
                    $data[$key]['reply'] = implode($replys,',');
                }else{
                    $data[$key]['reply'] = '';
                }

            }
            $cellname = [
                '报告ID', '用户姓名', '用户头像', '用户电话', '用户类型',
                '提交时间', '上报状态', '上报时间', '产品名称', '关联企业',
                '解决状态', '是否转让同事', '转让信息', '患者姓名', '性别',
                '出生年月', '联系方式', '原始疾病', '药物规格', '起始用药日期',
                '单次用药剂量', '其他合并用药', '不良事件名称', '时间发生日期', '不良事件结果',
                '不良事件描述', '图片', '音频','音频时长', '视频',
                '视频时长', '回复内容(姓名,手机号,回复时间,回复内容)'
            ];
            $status = ['user_type','sae_submit','sae_answer','sae_zhuan'];
            $values = [
                'user_type' => [1 => '医生',2 => '代表'],
                'sae_submit' => [1 => '未上报',2 => '已上报'],
                'sae_answer' => [1 => '未解决',2 => '已解决'],
                'sae_zhuan' => [1 => '未转让',2 => '已转让']
            ];
            $res = exportExcel('saedata',time(),$cellname,$data,$status,$values);
            return json($res);
        }else{
            return json(['code'=>0,'msg'=>'此条件下无数据']);
        }
    }
}